/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Facades;


import Entidades_Persistencia.Persona;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author joselvr
 */
@Stateless
public class PersonaFacade extends AbstractFacade<Persona> {
    @PersistenceContext(unitName = "EvaproyPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PersonaFacade() {
        super(Persona.class);
    }
    
    public List findByParams(String nombreFind, String apellidoFind, int cedulaFind, int codigoPerfil){
        List personaResul= (List)em.createQuery("SELECT p FROM Persona p JOIN p.codigoPerfil pf WHERE p.nombres = '"+nombreFind+"'"+
                " OR p.apellidos = '"+apellidoFind+"' OR p.cedula="+cedulaFind+" OR pf.codigo="+codigoPerfil)
                .getResultList();
        return personaResul;
    }
    
    public List findByLoginPasswd(String login, String passwd) {
        try {
              List loginn= (List)em.createQuery("SELECT p.login login ,p.contrasennia pasword , pf.codigo codPerfil, p.codigoInterno codPersona"
                                                     + " FROM Persona p JOIN p.codigoPerfil pf WHERE p.login = '"+login+"'  AND p.contrasennia= '"+passwd+"'")
                      //.setParameter("login", login).setParameter("contrasena", passwd)
                   //.getSingleResult();
                   .getResultList();
            
            
//            
//             List loginn= (List)em.createQuery("SELECT p.login , p.contrasennia"
//                                                     + " FROM Persona p WHERE "
//                                                     + " p.login = '"+login+"' AND p.contrasennia= '"+passwd+"'")
//                      //.setParameter("login", login).setParameter("contrasena", passwd)
//                     //.getSingleResult();
//                  .getResultList();

              return loginn;
              
              /*
            
            
            Persona persona= (Persona) em.createQuery("SELECT p FROM Persona p "
                                                     +"  WHERE p.login = :login AND p.contrasennia= :contrasena")
                      .setParameter("login", login).setParameter("contrasena", passwd)
                      .getSingleResult();

              return persona;*/
              
                } catch (NoResultException e) {
            return null;
                }
    }
      
      
      
//    public List buscarPorLogin(String login) {
//
//        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
//        CriteriaQuery cq = cb.createQuery();
//        Root<Persona> usus = cq.from(Persona.class);
//
//        cq.select(usus);
////.where(cb.equal(usus.get(Persona_.login), login));
//        Query q = em.createQuery(cq);
////q.setMaxResults(1);
//        return q.getResultList();
//    }
    
//    public List buscarPorLogin(String login) {
//        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
//        cq.select(cq.from(Persona.class));
//        javax.persistence.Query q = getEntityManager().createQuery(cq);
//        q.setMaxResults(1);
//        return q.getResultList();
//    }
     public List buscarEvaluador(int i) {
        List personasEvaluadores= (List)em.createQuery("SELECT p FROM Persona p "
                + "WHERE p.codigoPerfil.codigo="+i)
                .getResultList();
        return personasEvaluadores;
    
}
}
